/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/16
 * Time: 13:49
 */
$(function(){
    var ue = UE.getEditor('editor', {initialFrameHeight: 250});

    $('.btn-save').on('click', function(){
        $('[name="content"]').val(ue.getContent());
        var data = {
            name: $('.template-name').val(),
            desc: $('.template-desc').val()
        };
        if (checkForm(data)) {
            sendAjax('/v1/manager/save/template', 'post', {data:data}, function(json) {
                var content = '<span>'+json.message+'</span>';
                if (json.status == 200) content += '<input type="hidden" class="jump-url" value="/manager/contract/template.html">';
                $('#action-tip').find('.tip-content').append(content);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#action-tip').on('hide.bs.modal', function(){
        if ($(this).find('.jump-url').val()) {
            location.href = $(this).find('.jump-url').val();
        }
    });
});

function checkForm(data){
    if (!data.name) {
        $('#action-tip .modal-body .tip-content').text('请填写模板名称');
        $('#action-tip').modal('show');
        return false;
    }
    if (!data.desc) {
        $('#action-tip .modal-body .tip-content').text('请填写模板内容');
        $('#action-tip').modal('show');
        return false;
    }

    return true;
}